package com.liang.bishi.mega;

import java.util.HashMap;
import java.util.Map;

/**
 * 找出数组中只出现一次的数字
 */
public class Test {
    public static void main(String[] args) {
        int[] nums = new int[]{4, 1, 2, 1, 2};
        System.out.println(singleNumber(nums));
    }

    public static int singleNumber(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            if (map.containsKey(num)) {
                map.put(num, map.get(num) + 1);
            } else {
                map.put(num, 1);
            }
        }
        int res = 0;
        for (Integer key : map.keySet()) {
            Integer value = map.get(key);
            if (value == 1) {
                res = key;
                break;
            }
        }
        return res;
    }
}
